//
//  GradientView.m
//  Gradient
//
//  Created by S_Line on 2018/6/8.
//  Copyright © 2018年 S_Line. All rights reserved.
//

#import "GradientView.h"

@interface GradientView()

@property (nonatomic, weak) CAGradientLayer *gradientLayer;

@end

@implementation GradientView

+ (Class)layerClass {
    return [CAGradientLayer class];
}

- (CAGradientLayer *)gradientLayer {
    return (CAGradientLayer *)self.layer;
}

- (instancetype)init {
    self = [super init];
    
    if (self) {
    }
    
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    
    if (self) {
    }
    
    return self;
}

- (void)setColors:(NSArray *)colors {
    _colors = colors;
    
    NSMutableArray *colorsArray = [NSMutableArray array];
    if (colors && [colors isKindOfClass:[NSArray class]] && colors.count > 0) {
        for (int i = 0; i < colors.count; i++) {
            UIColor *color = colors[i];
            if (color && [color isKindOfClass:[UIColor class]]) {
                [colorsArray addObject:(__bridge id)color.CGColor];
            }
        }
    }
    self.gradientLayer.colors = colorsArray;
}

- (void)setLocations:(NSArray *)locations {
    _locations = locations;
    
    NSMutableArray *locationsArray = [NSMutableArray array];
    if (self.locations && [self.locations isKindOfClass:[NSArray class]] && self.locations.count > 0) {
        for (int i = 0; i < self.locations.count; i++) {
            NSObject *location = self.locations[i];
            if (location && [location isKindOfClass:[NSNumber class]]) {
                [locationsArray addObject:location];
            }
        }
    }
    self.gradientLayer.locations = locationsArray;
}

- (void)setStartPoint:(CGPoint)startPoint {
    _startPoint = startPoint;
    self.gradientLayer.startPoint = self.startPoint;
}

- (void)setEndPoint:(CGPoint)endPoint {
    _endPoint = endPoint;
    self.gradientLayer.endPoint = self.endPoint;
}

@end
